package com.yunbao.hujin.myguidepage.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.yunbao.hujin.myguidepage.R;
import com.yunbao.hujin.myguidepage.guide.GuideActivity;

/**
 * 引导页的Adapter on 2017/12/1.
 */

public class GuideAdapter extends PagerAdapter{

    private Context context;
    private ImageView imageView;    //存放图片
    private View btn;               //立即体验
    private int[] guideImg;

    public GuideAdapter(Context context, int[] guideImg) {
        this.context = context;
        this.guideImg = guideImg;
    }

    @Override
    public int getCount() {
        return guideImg.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = LayoutInflater.from(context).inflate(R.layout.fragment_guide, null);
        container.addView(view);
        imageView = view.findViewById(R.id.guide_img);
        btn = view.findViewById(R.id.btn);

        if (position == getCount() - 1) {   //最后一页时 "立即体验"显示
            btn.setVisibility(View.VISIBLE);
        } else {
            btn.setVisibility(View.GONE);
        }
        imageView.setImageResource(guideImg[position]);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((GuideActivity)context).startMainActivity();
            }
        });
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
}
